package com.dzgs.zw.model;

public class Tool {
	public static byte[] StringTopBytes(String strsrc) {
		int i;
		int ilen = (strsrc.length() + 1) / 2;
		byte bdest[] = new byte[ilen];
		char bsrc[] = strsrc.toCharArray();
		int ii = 0;
		for (i = 0; i < ilen; i++) {
			bdest[i] = (byte) ((((bsrc[2 * i] - 0x30) & 0x0f) << 4) + ((bsrc[2 * i + 1] - 0x30) & 0X0F));
		}
		return bdest;
	}
	
	public static String getHexStr(byte[] b){
		String str="";
		for(int i=0;i<b.length;i++){
			String hex = Integer.toHexString(b[i]&0xFF);
			if(hex.length()==1){
				hex='0'+hex;
			}
			str+=hex;
		}
		return str;
	}
	
	public static byte[] hexStringToBytes(String hexString) {
        if (hexString == null || hexString.equals("")) {
            return null;
        }
//        hexString = hexString.toUpperCase();
        int length = hexString.length() / 2;
        char[] hexChars = hexString.toCharArray();
        byte[] d = new byte[length];
        for (int i = 0; i < length; i++) {
            int pos = i * 2;
            d[i] = (byte) (charToByte(hexChars[pos]) << 4 | charToByte(hexChars[pos + 1]));
            
        }
        return d;
    }
	
	private static byte charToByte(char c) {
//        return (byte) "0123456789ABCDEF".indexOf(c);
		return (byte) "0123456789:;<=>?".indexOf(c);
    }
}
